
package ups.Common;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;

public class DataImporter {
	public static void main(String[] args) {
	}
	
	public DataImporter( String[] args ) {
		argParse = new ArgumentParser( args );
	}
	
	public static String DBHostName  = "localhost";
	public static int 	 DBPort		 =	3306;
	public static String DBName		 = "upsferry";
	public static String DBUsername	 = "scanfield";
	public static String DBPass		 = "tiger";
	
	/* Command Line Arguments */
	private static ArgumentParser argParse = null;
	protected String getArgumentForKey( String key ) {
		return argParse.valueForKey( key );
	}
	
	/* Preferences */
	private static File  						prefFile = null;
	private static Hashtable<String,String> 	prefTable = null;
	protected String getPreferenceForKey( String key ) {
		try {
			if( prefFile == null ) {
				/* Get a path to the preference file based of the current working directory */
				prefFile = new File( System.getProperty("user.dir"), getPrefsFile() );
				FileReader 		prefFileReader 	= new FileReader( prefFile );
				BufferedReader 	prefReader 		= new BufferedReader( prefFileReader );
				
				prefTable = new Hashtable<String,String>();
				String line = null;
				while( (line = prefReader.readLine()) != null ) {
					String[] keyAndValue = line.split("\\s*=\\s*"); 
					prefTable.put( keyAndValue[ 0 ], keyAndValue[ 1 ]);
				}
			}
		} catch( FileNotFoundException fileException ) {
			System.out.println("Couldn't Load Preferences File: " + prefFile.toString() );
			System.exit(-1);
		} catch( IOException ioException ) {
			System.out.println("Problem loading preferences file: " + prefFile.toString() );
			System.exit(-1);
		}
		
		if( !(prefTable.containsKey( key ))) {
			System.err.println("Couldn't find a preference for key " + key );
			return null;
		}
		
		return prefTable.get( key );
	}
	
	public String getPrefsFile() {
		String className = this.getClass().getName();
		className = className.replaceFirst( className.substring( 0, className.lastIndexOf(".")+1), "");
		return className + ".settings" ;
	}
	
	public static String[] stringsFromFile( File stringFile, String delimiter ) {
		String	result = "";
		try {
			FileReader 		stringFileReader 	= new FileReader( stringFile );
			BufferedReader 	stringReader 		= new BufferedReader( stringFileReader );
			String 			line				= null;
			while( (line = stringReader.readLine()) != null ) {
				result += line + "\n";
			}
		} catch( IOException ioException ) {
			System.out.println("Couldn't load string array from file" + stringFile.toString());
			System.exit(-1);
		}
		return result.split( delimiter );
	}
}
